import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import vueJsx from '@vitejs/plugin-vue-jsx'
import { createSvgIconsPlugin } from 'vite-plugin-svg-icons'

import { resolve } from 'path'
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'


import commonjs from '@rollup/plugin-commonjs'
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'

import vueDevTools from 'vite-plugin-vue-devtools'
import { lazyImport, VxeResolver } from 'vite-plugin-lazy-import'
import monacoEditorEsmPlugin from 'vite-plugin-monaco-editor-esm'

// https://vitejs.dev/config/
export default defineConfig({
  base: '',
  plugins: [
/*    vueDevTools({
      launchEditor: 'webstorm',
    }),*/
    commonjs(),
    vue(),
    //添加jsx/tsx支持d
    vueJsx({}),
    lazyImport({
      resolvers: [
        VxeResolver({
          libraryName: 'vxe-table',
        }),
        VxeResolver({
          libraryName: 'vxe-pc-ui',
        }),
      ],
    }),
    monacoEditorEsmPlugin({
      languageWorkers: ['editorWorkerService', 'typescript', 'json', 'html'],
      publicPath: 'http://localhost:81/monacoeditorwork',
    }),
    /* 开启externalGlobals后，
       报错：TypeError: Cannot read properties of null (reading 'nodeType')，不知何故？？ */
// externalGlobals({
//   vue: "Vue",
//   'element-plus': 'ElementPlus',
// }),
    AutoImport({
      imports: ['vue'],
    }),
    Components({
      extensions: ['vue', 'md'],
      // allow auto import and register components used in markdown
      include: [/\.vue$/, /\.vue\?vue/, /\.md$/],
      resolvers: [
        ElementPlusResolver({
          importStyle: 'sass',
        }),
      ],
    }),

    createSvgIconsPlugin({
      // Specify the icon folder to be cached
      iconDirs: [resolve(process.cwd(), 'src/icons/svg')],
      // Specify symbolId format
      symbolId: 'icon-[dir]-[name]',
    }),
  ],

  resolve: {
    alias: {
      '@':
        resolve(__dirname, 'src'), // 路径别名
    },
    extensions: ['.js', '.vue', '.json', '.ts', '.mjs'], // 使用路径别名时想要省略的后缀名，可以自己 增减
  },

  /*  optimizeDeps: {
      include: ['quill']
    },*/

  css: {
    preprocessorOptions: {
      scss: {
        /* 自动引入全局scss文件 */
        // @use "./src/styles/element/index.scss" as *;
        additionalData: `@use "@/styles/element/index.scss" as *;`,
        api: 'modern-compiler',
      },
    },
  },

  server: {
    host: '0.0.0.0',
    headers: {
      'Access-Control-Allow-Origin': '*', // 允许主应用域名访问
    },
    port: 3031,
    proxy: {
      '/api': {
        target: 'http://localhost:3000',
        changeOrigin: true,
        // rewrite: (path) => path.replace(/^\/api/, '')
      },
      /*'/monacoeditorwork':{
        target: 'http://localhost:81/monacoeditorwork',
        changeOrigin: true,
      }*/
    },
  },

  build: {
    //minify: false,
    commonjsOptions: {
      exclude: [
        // 'lib/vuedraggable/dist/vuedraggable.umd.js,',  //引号前的逗号不能删，不知何故？？
        //'vue/dist/*.js'
      ],
      include:
        [],
      //requireReturnsDefault: true
    },

    rollupOptions: {
      input: {
        designer: resolve(__dirname, 'index-designer.html'),
        render: resolve(__dirname, 'index-render.html')
      }

      // // 确保外部化处理那些你不想打包进库的依赖
      // external: ['vue', 'element-plus'],
      // output: {
      //   // 在 UMD 构建模式下为这些外部化的依赖提供一个全局变量
      //   globals: {
      //     vue: 'Vue', //报错：Failed to resolve module specifier "vue". Relative references must start with either
      //     'element-plus': 'ElementPlus',
      //   }
      // }
    },
  },
})
